<!-- post_submit.php -->
<?php
session_start();

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'db');

// 确保用户已登录
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }

    $user_id = $_SESSION['user_id'];
    $content = isset($_POST['content']) ? $_POST['content'] : '';
    $imageError = '';

    // 图片处理
    $image = '';
    $imageUrl = ''; // 初始化 imageUrl
    if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
        $imageTemp = $_FILES['image']['tmp_name'];
        $imageName = $_FILES['image']['name'];
        $imageSize = $_FILES['image']['size'];
        $imageExt = strtolower(pathinfo($imageName, PATHINFO_EXTENSION));

        $allowedTypes = ['jpg', 'jpeg', 'png', 'gif'];
        $maxFileSize = 5 * 1024 * 1024;

        if (!in_array($imageExt, $allowedTypes) || $imageSize > $maxFileSize) {
            $imageError = "仅支持上传" . implode(',', $allowedTypes) . "格式的图片，且大小不超过5MB。";
        } else {
            $uniqueName = uniqid() . '.' . $imageExt; // 在这里定义$uniqueName
            $uploadPath = "uploads/$uniqueName";

            if (move_uploaded_file($imageTemp, $uploadPath)) {
                if (file_exists($imageTemp)) {
                    unlink($imageTemp); // 删除临时文件
                }
                $image = $uniqueName;
                $imageUrl = "uploads/$uniqueName"; // 设置imageUrl
            } else {
                $imageError = "图片上传失败。";
            }
        }
    }

    if ($imageError) {
        echo $imageError;
        exit();
    }

    // 插入动态
    $sql = "INSERT INTO posts (content, image, image_url, user_id) VALUES (?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("sssi", $content, $image, $imageUrl, $user_id);

    if ($stmt->execute()) {
        echo "动态发布成功！";
    } else {
        echo "发布失败: " . $stmt->error;
    }

    $stmt->close();
    $conn->close();

    header("Location: profile.php");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发布动态</title>
    <style>
        /* ...CSS样式保持不变... */
    </style>
</head>
<body>
    <!-- ...HTML表单保持不变... -->
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发布动态</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f9;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            transition: background-color 0.5s ease-in-out;
        }

        .container {
            background-color: #fff;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            border-radius: 10px;
            padding: 20px;
            width: 400px;
            animation: fadeIn 1s ease-in-out forwards;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        textarea {
            width: 100%;
            height: 150px;
            resize: vertical;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            outline: none;
            transition: border-color 0.3s ease-in-out;
        }

        textarea:focus {
            border-color: #007BFF;
        }

        input[type="file"] {
            display: block;
            margin-top: 10px;
        }

        button {
            display: block;
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-top: 15px;
            transition: background-color 0.3s ease-in-out;
        }

        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <form action="post_submit.php" method="POST" enctype="multipart/form-data">
            <textarea name="content" placeholder="编写动态..." required></textarea>
            <input type="file" name="image" accept="image/*">
            <button type="submit">发布</button>
        </form>
    </div>
</body>
</html>